package com.vince.hulk.mechanism
{
import com.vince.hulk.character.Character;

import org.flixel.FlxObject;
import org.flixel.FlxSprite;

/**
 * 风扇
 * author yangwz
 */		
public class Fan extends FlxSprite implements IMechanism
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>Fan</code> instance.
	 * 
	 */
	public function Fan(X:Number=0, Y:Number=0, size:uint = 0, direction:int = 0, reach:int = 0, force:uint = 20)
	{
		super(X, Y, null);
		var fan:Class = null;
		var fanX:int = 0;
		var fanY:int = 0;
		fanPNG = fanPNG;
		fanPNGLeft = fanPNGLeft;
		fanPNGRight = fanPNGRight;
		_size = size;
		_direction = direction;
		_reach = reach;
		_x = X;
		_y = Y;
		_force = force;
		if (direction == FlxObject.LEFT)
		{
			fan = this.fanPNGLeft;
			fanX = 260;
			_reach = _reach * 2;
			fanY = 41;
		}
		else if (direction == FlxObject.RIGHT)
		{
			fan = this.fanPNGRight;
			fanX = 260;
			_reach = _reach * 2;
			fanY = 41;
		}
		else
		{
			fan = fanPNG;
			fanX = 41;
			fanY = 130;
		}
		loadGraphic(fan, true, true, fanX, fanY);
		addAnimation("blowMe", [0, 1], 4, true);
		solid = true;
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Embed(source = "assets/Fan.png")]
	private var fanPNG:Class;
	[Embed(source = "assets/FanLeft.png")]
	private var fanPNGLeft:Class;
	[Embed(source = "assets/FanRight.png")]
	private var fanPNGRight:Class;
	
	private var _size:uint;
	
	/**
	 * 方向 
	 */	
	private var _direction:uint;
	
	private var _reach:uint;
	
	private var _x:int;
	
	private var _y:int;
	
	private var _force:uint;
	//==========================================================================
	//  Methods
	//==========================================================================
	
	public function activate(character:Character):void
	{
		if (character.size() <= _size)
		{
			if (_direction == FlxObject.LEFT)
			{
				character.applyForce(-1 * ((_reach - (_x + width - (character.x + character.width))) / _reach) * _force);
			}
			else if (_direction == FlxObject.RIGHT)
			{
				character.applyForce((_reach - (character.x - _x)) / _reach * _force);
			}
			else if (_direction == FlxObject.UP)
			{
				character.velocity.y = character.velocity.y - (_reach - (_y - character.y)) / _reach * _force;
			}
			else if (_direction == FlxObject.DOWN)
			{
				character.velocity.y = character.velocity.y + (_reach - (_y - character.y)) / _reach * _force;
			}
		}
	}
	
	public function toggle():void
	{
	}
	
	override public function update():void
	{
		play("blowMe");
	}
}
}